package com.domain.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

import javax.swing.JOptionPane;

import com.domain.User;
import com.domain.impl.UserImpl;
import com.domain.service.UserService;
import com.domain.service.utils.ServiceUtils;
import com.views.ForgotPasswordView;

public class ForgotPasswordController {

	private ForgotPasswordView forgotPasswordView;
	private ActionListener actionListener;

	public ForgotPasswordController(ForgotPasswordView forgotPasswordView) {

		this.forgotPasswordView = forgotPasswordView;
	}

	public void controll() {

		ActionListener actionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Object obj = e.getSource();

				if (obj.equals(forgotPasswordView.getBtnSubmit())) {

					String userName = forgotPasswordView.getTxtUserName()
							.getText();
					String question = forgotPasswordView.getTxtQuestion()
							.getText();
					String answer = forgotPasswordView.getTxtAnswer().getText();

					if (userName.equals("") || question.equals("")
							|| answer.equals("")) {

						forgotPasswordView.getLblMessages().setText(
								"Fields with * are mandatory");
						forgotPasswordView.getLblMessages().setVisible(true);

						if (userName.equals("")) {
							forgotPasswordView.getLblUserNameError()
									.setVisible(true);
							forgotPasswordView.getTxtUserName().setBackground(
									Color.LIGHT_GRAY);
						} else {

							forgotPasswordView.getLblUserNameError()
									.setVisible(false);
							forgotPasswordView.getTxtUserName().setBackground(
									Color.white);
						}

						if (question.equals("")) {
							forgotPasswordView.getLblQuestionError()
									.setVisible(true);
							forgotPasswordView.getTxtQuestion().setBackground(
									Color.LIGHT_GRAY);
						} else {
							forgotPasswordView.getLblQuestionError()
									.setVisible(false);
							forgotPasswordView.getTxtQuestion().setBackground(
									Color.white);
						}
						if (answer.equals("")) {
							forgotPasswordView.getLblAnswerError().setVisible(
									true);
							forgotPasswordView.getTxtAnswer().setBackground(
									Color.LIGHT_GRAY);
						} else {
							forgotPasswordView.getLblAnswerError().setVisible(
									false);
							forgotPasswordView.getTxtAnswer().setBackground(
									Color.white);
						}

					} else {

						boolean success = false;
						UserService userService = ServiceUtils.getUserService();

						List<User> list = userService.getAll();

						for (int i = 0; i < list.size(); i++) {

							User user = list.get(i);

							if (user.getUserName().equals(userName)
									&& user.getQuestion().equals(question)
									&& user.getAnswer().equals(answer)) {

								forgotPasswordView.getLblMessages().setText(
										"Hi " + userName
												+ "!  Your password is "
												+ user.getPassword());
								forgotPasswordView.getLblMessages().setVisible(
										true);
								success = true;
							}

						}
						if (success == false) {

							forgotPasswordView.getLblMessages().setText(
									"Sorry! Please check your details!");

							forgotPasswordView.getLblMessages()
									.setVisible(true);
							forgotPasswordView.getLblUserNameError()
									.setVisible(false);

							forgotPasswordView.getLblQuestionError()
									.setVisible(false);
							forgotPasswordView.getLblAnswerError().setVisible(
									false);
							forgotPasswordView.getTxtUserName().setBackground(
									Color.white);
							forgotPasswordView.getTxtQuestion().setBackground(
									Color.white);
							forgotPasswordView.getTxtAnswer().setBackground(
									Color.white);
						}

					}
				}

				if (obj.equals(forgotPasswordView.getBtnReset())) {

					forgotPasswordView.getTxtUserName().setText("");
					forgotPasswordView.getTxtQuestion().setText("");
					forgotPasswordView.getTxtAnswer().setText("");
					forgotPasswordView.getTxtUserName().requestFocus();

					forgotPasswordView.getLblMessages().setVisible(false);
					forgotPasswordView.getLblUserNameError().setVisible(false);
					forgotPasswordView.getLblQuestionError().setVisible(false);
					forgotPasswordView.getLblAnswerError().setVisible(false);

					forgotPasswordView.getTxtUserName().setBackground(
							Color.white);
					forgotPasswordView.getTxtQuestion().setBackground(
							Color.white);
					forgotPasswordView.getTxtAnswer()
							.setBackground(Color.white);

				}
			}
		};

		forgotPasswordView.getBtnSubmit().addActionListener(actionListener);
		forgotPasswordView.getBtnReset().addActionListener(actionListener);
	}
}
